//index.js
const app = getApp()
const DB = wx.cloud.database();
let len = "";
let len2 = "";
Page({
    data: {
        type: '',

        orderItem: [],
        orderItem2: [],

        fenleitype: ['全部','hpv疫苗',"新冠疫苗", "轮状疫苗", "狂犬疫苗", '流感疫苗', "乙肝疫苗", "肺炎疫苗"]
    },
    onLoad: function () {

    },

    onShow() {

        len2 = this.data.orderItem2.length
        DB.collection('yimiao').where({
            zhanghao: wx.getStorageSync('user').zhanghao,
            mima: wx.getStorageSync('user').mima,
        }).skip(len).orderBy('time_order', 'desc').get().then(res => {
            let oldData = this.data.orderItem2;
            console.log("b", res)
            wx.hideLoading();
            this.setData({
                orderItem2: res.data
            })
            console.log("this", this.data.orderItem2)
        })

    },


    onChange(e) {
      this.setData({
          title:e.detail.title
      })
        if (e.detail.title === '全部') {
            DB.collection('yimiao').where({
                    zhanghao: wx.getStorageSync('user').zhanghao,
                    mima: wx.getStorageSync('user').mima,
                })
                .get()
                .then((res) => {
                    console.log(res)
                    this.setData({
                        orderItem2: res.data,
                    })
                })

        } else {
            DB.collection('yimiao').where({
                    zhanghao: wx.getStorageSync('user').zhanghao,
                    mima: wx.getStorageSync('user').mima,
                    yimiaotype: e.detail.title
                })
                .get()
                .then((res) => {
                    console.log(res)
                    this.setData({
                        orderItem2: res.data,
                    })
                })
        }

    },


    shanchu(e) {
        let that = this;
        wx.showModal({
            title: '温馨提示',
            content: '是否删除？',
            confirmText: "是",
            cancelText: "否",
            success(res) {
                if (res.confirm) {
                    wx.showLoading({
                        title: '删除中',
                    })
                    wx.cloud.callFunction({
                        name: "update",
                        data: {
                            action: "shanchuym",
                            id: e.currentTarget.dataset.id,
                        }
                    }).then(res => {
                        wx.hideLoading({
                            success: (res) => {},
                        })

                        if (that.data.title === '全部') {
                            DB.collection('yimiao').where({
                                    zhanghao: wx.getStorageSync('user').zhanghao,
                                    mima: wx.getStorageSync('user').mima,
                                })
                                .get()
                                .then((res) => {
                                    console.log(res)
                                    that.setData({
                                        orderItem2: res.data,
                                    })
                                })
                
                        } else {
                            DB.collection('yimiao').where({
                                    zhanghao: wx.getStorageSync('user').zhanghao,
                                    mima: wx.getStorageSync('user').mima,
                                    yimiaotype: that.data.title
                                })
                                .get()
                                .then((res) => {
                                    console.log(res)
                                    that.setData({
                                        orderItem2: res.data,
                                    })
                                })
                        }

                    })
                } else if (res.cancel) {
                }
            }
        })
    },


    goDetail(e) {
        wx.navigateTo({
            url: '../yimiaodetail/index?id=' + e.currentTarget.dataset.id,
        })
    },

})